Page({
  data: {
    userName: "", //用户名
    headImg: "",
    home: "", //家庭地址
    schoolId: "", //学号
    call: "", //电话
    schoolPicker: ["成都文理学院", "西南交通大学希望学院", "成都泛美航空旅游中等职业技术学校"]
  },

  onShow() {
    wx.showLoading({
      title: '数据加载中...',
    });

    var that = this;
    const openid = wx.getStorageSync('openId'); // 直接获取 openid
    
    // 检查 openid 是否存在
    if (!openid) {
      wx.hideLoading();
      wx.showToast({
        title: '用户信息获取失败，请重新登录',
        icon: 'none'
      });
      return;
    }

    const db = wx.cloud.database({
      env: "cloud-found-5gjykn4j0bc454c0"
    });
    
    db.collection('user').where({
      _openid: openid // 直接使用存储的 openid
    }).get({
      success: function(res) {
        if (res.data && res.data.length > 0) {
          that.setData({
            userName: res.data[0].userName,
            headImg: res.data[0].headImg,
            home: res.data[0].home,
            schoolId: res.data[0].schoolId,
            call: res.data[0].call
          });
        } else {
          wx.showToast({
            title: '用户信息不存在',
            icon: 'none'
          });
        }
        wx.hideLoading();
      },
      fail: function(err) {
        console.error('查询用户信息失败:', err);
        wx.hideLoading();
        wx.showToast({
          title: '数据加载失败',
          icon: 'none'
        });
      }
    });
  },

  headImg() {
    const openid = wx.getStorageSync('openId');
    const userinfo = wx.getStorageSync('userinfo');
    
    if (!openid || !userinfo) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      });
      return;
    }

    if (this.data.headImg !== userinfo.avatarUrl) {
      const db = wx.cloud.database({
        env: "cloud-found-5gjykn4j0bc454c0"
      });
      
      db.collection('user').where({
        _openid: openid // 直接使用存储的 openid
      }).update({
        data: {
          headImg: userinfo.avatarUrl,
        },
        success: function(res) {
          wx.showModal({
            title: "修改成功",
            content: "点击返回上一页",
            showCancel: true,
            cancelColor: "#000000",
            confirmText: "确认",
            confirmColor: "#576B95",
            complete: function() {
              wx.navigateBack({
                delta: 1
              });
            }
          });
        },
        fail: function(err) {
          console.error('更新头像失败:', err);
          wx.showToast({
            title: '修改失败',
            icon: 'none'
          });
        }
      });
    } else {
      wx.showToast({
        title: "拾光驿站：您的头像与微信头像一致无需修改",
        icon: "none",
        duration: 2000,
        mask: false,
      });
    }
  },

  schoolPicker(e) {
    const openid = wx.getStorageSync('openId');
    
    if (!openid) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      });
      return;
    }

    const selectedSchool = this.data.schoolPicker[e.detail.value];
    this.setData({
      schoolId: selectedSchool
    });
    
    const db = wx.cloud.database({
      env: "cloud-found-5gjykn4j0bc454c0"
    });
    
    db.collection('user').where({
      _openid: openid // 直接使用存储的 openid
    }).update({
      data: {
        schoolId: selectedSchool,
      },
      success: function(res) {
        wx.showToast({
          title: "拾光驿站：修改成功",
          icon: "none",
          duration: 1500,
          mask: false,
        });
      },
      fail: function(err) {
        console.error('更新学校失败:', err);
        wx.showToast({
          title: '修改失败',
          icon: 'none'
        });
      }
    });
  }
});